package com.gdk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.gdk.service.ArticleService;
import com.gdk.service.FairService;
import com.gdk.service.HotelService;
import com.gdk.service.LoginService;
import com.gdk.service.NavigationService;
import com.gdk.service.PromotionService;
import com.gdk.service.ShopService;
import com.gdk.utils.HUtil;
import com.gdk.utils.MapTypeAdapter;
import com.gdk.utils.XVDateUtils;

/**
 * 会议controller
 * */

@Controller
public class FairController {
	
	@Autowired
    private FairService fairService;
	@Autowired
    private HotelService hotelService;
	/**
	 * 查询出所有酒店
	 * */
	@RequestMapping("/preAddFair.do")
	public String preAddFair(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		List<Map<String, Object>> allHotel = hotelService.selectAllHotel(HUtil.putInMap("hrn_status",0));
		request.setAttribute("allHotel", allHotel); 
		return "jsp/fair/fair_add";
	}
	
	/**
	 * 新增会议
	 * */
	@ResponseBody
	@RequestMapping("/addFair.do")
	public int addFair(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		map.put("f_status", 0);
		map.put("f_addtime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("会议插入  :  "+map);

		
		if(map.get("hytime") != null && !"".equals(map.get("hytime").toString())) {
			String hytime = map.get("hytime").toString();
			
			Map<String, Object> dateCutMillis = XVDateUtils.dateCutMillis(hytime);
			
			map.put("f_start_time", dateCutMillis.get("f_time"));
			map.put("f_end_time", dateCutMillis.get("e_time"));
		}
		
		int re_status = fairService.addFair(map);
		//int re_status = 0;
		System.out.println("结果 : "+re_status);
		
		
		if(re_status == 1) {
			String hrn_id_str = map.get("hrn_id").toString();
			
			String[] hrn_id_arr = hrn_id_str.split(",");
			for (String hrn_id_s : hrn_id_arr) {
				Map<String,Object> bindMap = new HashMap<String,Object>();
				bindMap.put("hf_ctime",XVDateUtils.getCurrenTimeMillis());
				bindMap.put("f_id",map.get("f_id"));
				bindMap.put("hrn_id",Integer.valueOf(hrn_id_s));
				fairService.bindFairAndHotel(bindMap);
			}
			
			
		}
		
		
	
		
		
		return re_status;
	}
	
	/**
	 * 查询所有会议
	 * */
	@RequestMapping("/findAllFair.do")
	public String findAllFair(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);
		
	
		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allFair = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = fairService.findAllFair(map).size();
		
		map.put("allArticleItem",itemCount);
		
		
		
		
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/fair/fair_manage";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allFair = fairService.findAllFair(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allFair) {
			if(map2.get("f_addtime") != null) {
				int p = Integer.valueOf(map2.get("f_addtime").toString());
				if(p != 0) {
					map2.put("f_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("f_start_time") != null) {
				int p = Integer.valueOf(map2.get("f_start_time").toString());
				if(p != 0) {
					map2.put("f_start_time", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("f_end_time") != null) {
				int p = Integer.valueOf(map2.get("f_end_time").toString());
				if(p != 0) {
					map2.put("f_end_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
			//查询出关联酒店信息
			List<Map<String, Object>> bindFairAndHotel = fairService.findBindFairAndHotel(map2);
			map2.put("bindFairAndHotel", bindFairAndHotel);
		}
		
		/*List<Map<String, Object>> allModule = navigationService.selectAllModule();
		request.setAttribute("allModule", allModule);*/
		request.setAttribute("allFair",allFair);
		
		
		
		
		return "jsp/fair/fair_manage";
	}
	
	
	
	
	/**
	 *修改前查询会议详情
	 * */
	@RequestMapping("/findFairDetails.do")
	public String findFairDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> fairDetails = fairService.findFairDetails(map);
		
		if(fairDetails.get("f_start_time") != null) {
			int p = Integer.valueOf(fairDetails.get("f_start_time").toString());
			if(p != 0) {
				fairDetails.put("f_start_time", XVDateUtils.MillisFormat(p));	
			}
		}
		if(fairDetails.get("f_end_time") != null) {
			int p = Integer.valueOf(fairDetails.get("f_end_time").toString());
			if(p != 0) {
				fairDetails.put("f_end_time", XVDateUtils.MillisFormat(p));	
			}
		}
		
		request.setAttribute("fairDetails",fairDetails);
		
		
		List<Map<String, Object>> bindFairAndHotel = fairService.findBindFairAndHotel(map);
		
		request.setAttribute("bindFairAndHotel", bindFairAndHotel); 
		List<Map<String, Object>> allHotel = hotelService.selectAllHotel(map);
		request.setAttribute("allHotel", allHotel); 
		return "jsp/fair/fair_update";
	}
	
	/**
	 * 修改会议状态
	 * */
	@ResponseBody
	@RequestMapping("/updateFairStatus.do")
	public int updateFairStatus(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("文章 : "+map);
		int shop = fairService.updateFairStatus(map);
		return shop;
	}

	/**
	 * 修改会议详情
	 * */
	@ResponseBody
	@RequestMapping("/updateFairDetails.do")
	public int updateFairDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("会议修改 : "+map);
		if(map.get("hytime") != null && !"".equals(map.get("hytime").toString())) {
			String hytime = map.get("hytime").toString();
			
			Map<String, Object> dateCutMillis = XVDateUtils.dateCutMillis(hytime);
			
			map.put("f_start_time", dateCutMillis.get("f_time"));
			map.put("f_end_time", dateCutMillis.get("e_time"));
		}
		
		int shop = fairService.updateFairDetails(map);

		if(shop == 1) {
			if(map.get("hrn_id") != null && !"".equals(map.get("hrn_id").toString())) {
				//清空指定会议
				fairService.deleteBindFairAndHotel(map);
				
				String hrn_id_str = map.get("hrn_id").toString();
				String[] hrn_id_arr = hrn_id_str.split(",");
				
				for (String hrn_id_ssss : hrn_id_arr) {
					int hrn_id = Integer.valueOf(hrn_id_ssss);
					Map<String,Object> bindMap = new HashMap<String,Object>();
					
					bindMap.put("hf_ctime",XVDateUtils.getCurrenTimeMillis());
					bindMap.put("f_id",map.get("f_id"));
					bindMap.put("hrn_id",hrn_id);
					
					fairService.bindFairAndHotel(bindMap);
				}
				
			}
		}
		
		
		//修改酒店会议绑定
		//查询酒店绑定
		//Map<String, Object> fairDetails = fairService.findFairDetails(map);
		//List<Map<String, Object>> bindFairAndHotel = fairService.findBindFairAndHotel(map);
		
		//for (Map<String, Object> map2 : bindFairAndHotel) {
			
			
			
			
			/*if(map2.get("hrn_id") != null) {
				int pre_hrn_id = Integer.valueOf(map2.get("hrn_id").toString()); 
				//不一致修改
				int hrn_id = Integer.valueOf(map.get("hrn_id").toString());
				
				if(pre_hrn_id != hrn_id) {
					fairService.updateBindFairAndHotel(HUtil.putInMap("hf_id",fairDetails.get("hf_id"),"hrn_id",map.get("hrn_id"),"hf_ctime",XVDateUtils.getCurrenTimeMillis()));
				}
			}else {
				Map<String,Object> bindMap = new HashMap<String,Object>();
				
				bindMap.put("hf_ctime",XVDateUtils.getCurrenTimeMillis());
				bindMap.put("f_id",map.get("f_id"));
				bindMap.put("hrn_id",map.get("hrn_id"));
				
				fairService.bindFairAndHotel(bindMap);
			}*/
	//	}
		
		
		
		
		
		return shop;
	}

/**
 * 所有会议接口
 * 
 * 接口格式
 * {
  "code": 0
  ,"msg": ""
  ,"count": 3000000
  ,"data": [{
    "id": "10001"
    ,"username": "杜甫"
  },{
    "id": "10002"
    ,"username": "礼拜"
  }]
 * */
	@ResponseBody
	@RequestMapping("/selectAllFairJson.do")
	public String selectAllFairJson(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		
		List<Map<String, Object>> allFair = fairService.findAllFair(map);
		if(allFair.size() > 0) {
			resultMap.put("code", 0);
			resultMap.put("msg", "可选会议");
			resultMap.put("count",allFair.size());
			resultMap.put("data", allFair);
		}else {
			resultMap.put("code", 1);
			resultMap.put("msg", "无会议");
			resultMap.put("count", 0);
			resultMap.put("data", null);
		}
		
		Gson gson = new GsonBuilder()
                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
                }.getType(), new MapTypeAdapter()).create();
		String json = gson.toJson(resultMap);
		
		return json;
	}
	
	
	/**
	 * 所有会议包含用户接口
	 * 
	 * 接口格式
	 * {
	  "code": 0
	  ,"msg": ""
	  ,"count": 3000000
	  ,"data": [{
	    "id": "10001"
	    ,"username": "杜甫"
	  },{
	    "id": "10002"
	    ,"username": "礼拜"
	  }]
	 * */
		@ResponseBody
		@RequestMapping("/findAllFairAndUserJson.do")
		public String findAllFairAndUserJson(@RequestParam Map<String,Object> map, HttpServletRequest request) {
			Map<String,Object> resultMap = new HashMap<String,Object>();
			
			List<Map<String, Object>> allFair = fairService.findAllFairAndUser(map);
			if(allFair.size() > 0) {
				resultMap.put("code", 0);
				resultMap.put("msg", "可选会议");
				resultMap.put("count",allFair.size());
				resultMap.put("data", allFair);
			}else {
				resultMap.put("code", 1);
				resultMap.put("msg", "无会议");
				resultMap.put("count", 0);
				resultMap.put("data", null);
			}
			
			Gson gson = new GsonBuilder()
	                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
	                }.getType(), new MapTypeAdapter()).create();
			String json = gson.toJson(resultMap);
			
			return json;
		}
	
		
		
		/**
		 * 删除会议
		 * */
		@ResponseBody
		@RequestMapping("/delFair.do")
		public int delFair(@RequestParam Map<String,Object> map, HttpServletRequest request) {
			int i = fairService.delFair(map);
			if(i == 1) {
				//删除酒店会议绑定
				fairService.deleteBindFairAndHotel(map);
			}
			return i;
		}
}